package com.xiaojian.pick.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaojian.pick.entity.ApplyBond;
import com.xiaojian.pick.entity.Commodity;
import com.xiaojian.pick.entity.User;
import com.xiaojian.pick.mapper.ApplyBondMapper;
import com.xiaojian.pick.service.ApplyBondService;
import com.xiaojian.pick.service.CommodityService;
import com.xiaojian.pick.service.UserService;
import com.xiaojian.pick.service.WxMessageService;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author whycode
 * @title: ApplyBondServiceImpl
 * @projectName campus-secondary-market-app---sys
 * @description: TODO
 * @date 2022/2/2015:03
 */

@Service
@Data
public class ApplyBondServiceImpl extends ServiceImpl<ApplyBondMapper, ApplyBond> implements ApplyBondService {

    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Value("${apply.temp}")
    private String applyTemp;

    @Resource
    private WxMessageService wxMessageService;

    @Resource
    private UserService userService;

    @Resource
    @Lazy
    private CommodityService commodityService;

    @Override
    public boolean save(ApplyBond entity) {
        boolean save = super.save(entity);
        if(save){
            User user = userService.findById(entity.getUserId());
            User apply = userService.findById(entity.getApplicant());
            Commodity commodity = commodityService.findById(entity.getCommodity());
            Map<String, Object> msg = new HashMap<>();
            msg.put("touser", user.getOpenid());
            msg.put("template_id", applyTemp);
            msg.put("miniprogram_state", "trial");
            msg.put("lang", "zh_CN");
            Map<String, Msg> data = new HashMap<>();
            data.put("thing1", new Msg(apply.getNickName() + "申请结缘"));
            data.put("time3", new Msg(LocalDateTime.now().format(FORMATTER)));
            data.put("thing3", new Msg(entity.getRemark()));
            msg.put("data", data);
            wxMessageService.send(msg);
        }
        return save;
    }

    @Override
    public Map<String, Object> list(ApplyBond applyBond) {
        if(applyBond.getPage() != null && applyBond.getLimit() != null){
          applyBond.setStart((applyBond.getPage() - 1) * applyBond.getLimit());
        }

        QueryWrapper<ApplyBond> wrapper = new QueryWrapper<>();

        if(applyBond.getStatus() != null){
            wrapper.eq("status", applyBond.getStatus());
        }
        if(applyBond.getUserId() != null){
            wrapper.eq("user_id", applyBond.getUserId());
        }

        if(applyBond.getApplicant() != null){
            wrapper.eq("applicant", applyBond.getApplicant());
        }

        if(applyBond.getCommodity() != null){
            wrapper.eq("commodity", applyBond.getCommodity());
        }

        int count = count(wrapper);
        List<ApplyBond> list = getBaseMapper().list(applyBond);
        Map<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("count", count);
        return result;
    }

    @Override
    public boolean updateStatus(Long id, Short status, String refuse) {
        UpdateWrapper<ApplyBond> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set(refuse != null && !"".equals(refuse), "refuse", refuse);
        updateWrapper.set("status", status)
                .set("response_time", new Date())
                .eq("id", id);
        return this.update(new ApplyBond(), updateWrapper);
    }


    @Data
    @AllArgsConstructor
    class Msg{
        String value;
    }
}
